﻿/****************************************************************
* MACHINE: DESKTOP-I6HF1HG
* NAME: gjp
* CREATEDATE: 2019/3/14 17:54:49
* DESC: <DESCRIPTION>
* **************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LEE.Tools.Data
{
    /// <summary>
    /// Summary description for DictionaryHelper
    /// </summary>
    public static class DictionaryHelper
    {
        public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue value)
        {
            if (dic.ContainsKey(key))
            {
                dic[key] = value;
            }
            else
            {
                dic.Add(key, value);
            }
        }

        public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dic, KeyValuePair<TKey, TValue> keyValue)
        {
            AddOrUpdate(dic, keyValue.Key, keyValue.Value);
        }

        public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dic, IEnumerable<KeyValuePair<TKey, TValue>> keyValueList)
        {
            var list = keyValueList?.ToList();
            list?.ForEach(keyValue => { AddOrUpdate(dic, keyValue); });
        }
    }
}
